﻿@page
@model FytSoa.Web.Pages.FytAdmin.Sys.AdminToRoleModel
@{
ViewData["Title"] = "为用户分配角色";
Layout = "~/Pages/FytAdmin/_Layout.cshtml";
}
<style>
    .layui-table-view {
        margin-top: 0px;
    }

    .layui-col-220 {
        width: 200px;
    }

    .right-col-body {
        left: 200px;
    }

    .list-wall {
        padding-top: 0px;
    }

    .fyt-center {
        text-align: center;
    }

    #tree {
        height: 350px;
        overflow: auto;
    }

    .layui-layer-loading {
        box-shadow: 0 0px 0px rgba(0, 0, 0, .3) !important;
    }
</style>
<div class="layui-col-220">
    <div id="tree" class="layui-tree-cus" style="overflow:auto;"></div>
</div>
<div class="right-col-body">
    <div class="list-wall" style="margin-top:15px;">
        <table class="layui-hide" id="tablist" lay-filter="tool"></table>
    </div>
</div>
<script>
    layui.config({
        base: '/themes/js/modules/'
    }).use(['table', 'layer', 'jquery', 'tree', 'common'],
        function () {
            var table = layui.table,
                layer = layui.layer,
                $ = layui.jquery,
                tree = layui.tree,
                os = layui.common,
                form = layui.form;

            $("#tree").css({ 'height': $(window).height() - 140 });

            table.render({
                elem: '#tablist',
                headers: os.getToken(),
                url: '/api/role/torolelist?adminGuid=@Model.adminGuids',
                cols: [
                    [
                        {
                            field: 'name', title: '角色名称', sort: true, fixed: 'left', templet: function (d) {
                                if (d.level == 0) {
                                    return d.name + '<span class="layui-badge layui-bg-cyan">组</span>';
                                } else {
                                    return d.name;
                                }
                            }
                        },
                        { field: 'sort', title: '排序' },
                        { width: 150, title: '操作', templet: '#tool' }
                    ]
                ],
                page: false,
                height: $(window).height() - 30,
                where: {
                    limit: 1000
                },
                id: 'tables'
            });

            var guid = '', typeName = '', active = {
                initTree() {
                    os.ajax('api/organize/gettree/', {}, function (res) {
                        tree.render({
                            elem: '#tree'
                            , data: res
                            , onlyIconControl: true
                            , click: function (obj) {
                                guid = obj.data.id;
                                active.reload();
                            }
                        });
                    });
                },
                reload: function () {
                    table.reload('tables',
                        {
                            page: false,
                            where: {
                                key: guid,
                                limit: 1000
                            }
                        });
                }
            };
            active.initTree();
            $('.list-search .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //监听授权操作
            form.on('switch(torole)', function (obj) {
                var strRole = this.value;
                //开始提交信息
                var index = layer.load(1, {
                    shade: [0.1, '#fff']
                });
                os.ajax("api/rolemenu/torole", { AdminGuid: "@Model.adminGuids", RoleGuid: strRole, Types: 2, status: obj.elem.checked }, function (res) {
                    layer.close(index);
                    if (res.statusCode === 200) {
                        if (obj.elem.checked) {
                            os.success('授权成功~');
                        }
                        else {
                            os.success('取消授权~');
                        }
                    } else {
                        os.error(res.message);
                    }
                });
            });
        });
</script>
<script type="text/html" id="tool">
    {{#  if(d.level === 1){ }}
    <input type="checkbox" name="status" value="{{d.guid}}" lay-skin="switch" lay-text="取消|授权" lay-filter="torole" {{ d.status ? 'checked' : '' }}>
    {{#  } }} 
</script>